@file:Suppress("UNREACHABLE_CODE")

package com.mywork.mvpkodeingithub.rx

import androidx.fragment.app.FragmentActivity
import com.github.qingmei2.core.GlobalErrorTransformer

object GlobalErrorProcessor {

    /**
     * Status code
     */
    const val STATUS_OK = 200
    const val STATUS_UNAUTHORIZED = 401

    fun <T> processGlobalError(fragmentActivity: FragmentActivity): GlobalErrorTransformer<T> = GlobalErrorTransformer(
/*

        // 通过onNext流中数据的状态进行操作
        globalOnNextInterceptor = {
            when (it.statusCode) {
                STATUS_UNAUTHORIZED -> Observable.error(
                    Errors.AuthorizationError(timeStamp = System.currentTimeMillis())
                )
                else -> Observable.just(it)
            }
        },

        // 通过onError中Throwable状态进行操作
        globalOnErrorResume = { error ->
            when (error) {
                is ConnectException ->
                    Observable.error<T>(Errors.ConnectFailedException)
                // 这个错误会在onErrorRetrySupplier()中处理
                is Errors.AuthorizationError -> Observable.error<T>(error)
                else -> Observable.error<T>(error)
            }
        },

        retryConfigProvider = { retrySupplierError ->
            when (retrySupplierError) {
                // 网络连接异常，弹出dialog，并根据用户选择结果进行错误重试处理
                Errors.ConnectFailedException ->
                    RetryConfig.simpleInstance {
                        //RxDialog.showErrorDialog(fragmentActivity, "ConnectException")
                    }
                // 用户认证失败，弹出login界面
                is Errors.AuthorizationError ->
                    RetryConfig.simpleInstance {
                        val waitLogin = AuthorizationErrorProcessResult.WaitLoginInQueue(
                            lastRefreshStamp = retrySupplierError.timeStamp
                        )
                        AuthorizationErrorProcessor
                            .processTokenExpiredError(fragmentActivity, waitLogin)
                            .retryWhen {
                                it.flatMap { processorError ->
                                    when (processorError) {
                                        is AuthorizationErrorProcessResult.WaitLoginInQueue ->
                                            Observable.timer(50, TimeUnit.MILLISECONDS)
                                        else -> Observable.error(processorError)
                                    }
                                }
                            }
                            .onErrorReturn { processorError ->
                                when (processorError) {
                                    is AuthorizationErrorProcessResult.LoginSuccess -> true
                                    is AuthorizationErrorProcessResult.LoginFailed -> false
                                    else -> false
                                }
                            }
                            .firstOrError()
                    }
                else -> RetryConfig.none()      // 其它异常都不重试
            }
        },

        globalDoOnErrorConsumer = { error ->
            when (error) {
                is JSONException -> {
                    Toast.makeText(fragmentActivity, "$error", Toast.LENGTH_SHORT).show()
                    Log.w("rx stream Exception", "Json解析异常:${error.message}")
                }
                else -> Log.w("rx stream Exception", "其它异常:${error.message}")
            }
        }
*/

    )
}